/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2021/06/06
 * Description        : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"

/* Global typedef */

/* Global define */

/* Global Variable */

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
#include "ch32v20x.h"                  // Device header
#include "OLED.h"
#include "Motor.h"
//#include "Key.h"
#include "LED.h"
#include "Buzzer.h"
//#include "Serial.h"//
#include "Servo.h"//
#include "encoder.h"
#include "Timer.h"
#include "bsp_pid.h"
#include "linefollow.h"
//uint8_t KeyNum;//
uint8_t Serial_RxData;//
uint8_t RxData;
float Angle;
uint8_t flag=0;//
uint8_t bflag=0;//
uint8_t bflag1=0;
extern int8_t lspeed;//
extern int8_t rspeed;//
//extern float rotateSpeed1;//
//extern float rotateSpeed2;//
extern uint8_t flag_circle;//
//
int main(void)
{
    Delay_Init();
    LED_Init();
    Buzzer_Init();
    OLED_Init ();
    Motor_Init ();//
    //Key_Init ();
//    Serial_Init();
    Encoder_Init_Tim2();
    Encoder_Init_Tim3();
    Timer4_Init();//
    PID_param_init();//
    line_follow_init();//
    Motor_Left_SetSpeed(80);//
    Motor_Right_SetSpeed(80);//
    set_pid_target(220.0);//
    set_p_i_d(0.8,0.1,0.0);//
    //Servo_Init();
//    OLED_ShowString (1,1,"Speed:");
//    OLED_ShowString (2,1,"Set:");
//    OLED_ShowString (3,1, "RxData:");
//    OLED_ShowString (4,1, "Angle:");
    Angle=27.5;//
    Servo_SetAngle(Angle);//
    LED1_ON();
    flag=0;//
    flag_circle=0;//
    while(flag==0)
    {
        //KeyNum =Key_GetNum ();
        line_follow();//
        if(flag_circle==2 &&  bflag==0){LED1_OFF();Buzzer_ON();Delay_Ms(500);Buzzer_OFF();bflag=1;}
        if(flag_circle>=3){flag=3;break;}
//        OLED_ShowSignedNum (1,9,rotateSpeed1*60,3);
//        OLED_ShowSignedNum (2,9,220,3);
//        OLED_ShowSignedNum (4,9,Angle,3);//
        //printf("%f,%f\n",(rotateSpeed1*60),(rotateSpeed2*60));

    }
//    while(flag==2){
//        OLED_ShowSignedNum (1,9,rotateSpeed1*60,3);
//        OLED_ShowSignedNum (2,9,220,3);
//        OLED_ShowSignedNum (4,9,Angle,3);}//
    while(flag==3)//
    {
        stop();
            if(bflag1==0){
            Buzzer_ON();Delay_Ms(500);
            Buzzer_OFF();Delay_Ms(500);
            Buzzer_ON();Delay_Ms(500);
            Buzzer_OFF();
            bflag1=1;}
    }
}

//void USART2_IRQHandler(void)//
//{
//    if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
//    {
//        flag=2;
//        Serial_RxData = USART_ReceiveData(USART2);
//        RxData = Serial_RxData;
//        Serial_SendByte(RxData);
//        if(RxData==0x47)go_ahead();
//        if(RxData==0x4B)go_back();
//        if(RxData==0x48)turn_left();
//        if(RxData==0x4A)turn_right();
//        if(RxData==0x45)stop();
//        OLED_ShowHexNum(3, 9, RxData, 2);
//        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
//    }
//}
